<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;

header('content-type:text/html;charset=utf8');
class Specialty extends Controller{
    public function specialty(){
        //特产后台
    return view('specialty');
    }
    //添加方法
    public function graph_index()
    {
        $data = $_POST;
        $a=$_POST['address_t'];
//        dump($a);
        $a= implode('-',$a);
        $data['address_t'] = $a;
        $files = request()->file('img');//多图上传
//        dump($files);
        $cc="";
        foreach($files as $file) {
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if ($info) {
                $cc .= $info->getSaveName()."/" ;
                $newstr = substr($cc,0,strlen($cc)-1);
            } else {
                return $this->success('添加失败');
            }
        }
        $data['img'] = $newstr;
        $data = Db::table('special') ->insert($data);
        if (!$data) {
            $this->error('失败', 'specialty', '', 1);
        } else {
//            return view('graph_morris');
            $this->Redirect('Specialty/graph_morris');
        }
    }
    //显示6行分页
        public function graph_morris(){

           $list= Db::table('special')->order('id desc')->limit(5)->paginate(6);
//            dump($list);
        $page = $list->render();
            $cc = array();
            for($i=0;$i<count($list);$i++){
                $cc[$i]['img'] = explode("/",$list[$i]["img"]);
            }
//            dump($list);
            return view('graph_morris',['list'=>$list,'page'=>$page,'cc'=>$cc]);
        }
    //通过ID删除这条数据
    public function specdelete(){
            $id=$_GET["id"];
            $data = Db::table("special")->where("id=$id")->delete();//方法
            if($data){
                $this->success("YES","Specialty/graph_morris");
            }else{
                $this->error("NO","Specialty/graph_morris");
            }
        }
    //修改页面1把表中的数据传过来在修改页面2进行修改
    public function  specxg(){
        $get= $_GET['id'];
        $list = Db::name('special')->where('id',$get)->paginate();
        $cc = array();
        for($i=0;$i<count($list);$i++){
            $cc[$i]['img'] = explode("/",$list[$i]["img"]);
        }
        $this->assign('li', $cc);
        $this->assign('list', $list);
        return $this->fetch('specxg');
    }
    //修改页面2
    public function  specxg_app(){
            $get= $_GET['id'];
            $data =$_POST;
            $a=$_POST['address_t'];
            $file = request()->file('img');
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            $data['img'] = $info->getSaveName();
            $a= implode('-',$a);
            $data['address_t'] = $a;
        $asd=Db::table('special')->where('id', $get)->update($data);
            if ($asd) {
                $this->success('YES', 'graph_morris');
            } else {
                $this->error('NO');
            }
        }
    //模糊查询
    public function speccz(){
        $name = $_POST["key"];
        if($name==""){//判断name值是否为空
            $list = Db::name('special')->paginate();
            $this->assign('list', $list);
            return $this->fetch('graph_morris');
        }else{//不否为空进行模糊查询
            $list = Db::table("special")->where("
                name like '%$name'
                or name like '%$name'
                or name like '%$name%'
                or address like '%$name'
                or address like '%$name'
                or address like '%$name%'
                ")->paginate();  //查询 细分类的表 找到对应的类名
             $page = $list ->render();
        }
$this->assign('list', $list);
$this->assign('page', $page);
return $this->fetch('graph_morris');
    }
    //特产订单分页
    public function specdd(){
        $list = Db::name('dingdanbiao')->order('id desc')->limit(5)->paginate(9);//加入了一个倒序
        $this->assign('list', $list);
        $page=$list->render();
        return view('Specialty/specdd',['list'=>$list,'page'=>$page]);
    }

}

